package com.core.controller;

import com.core.domain.Roles;
import com.core.resp.AjaxResponse;
import com.core.service.RolesService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.core.annotation.OperationLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author : HUANG
 * @create : 2025/6/1 16:28
 * @function :
 */
@RestController
@RequestMapping("/roles")
@Api("角色获取")
public class RolesController {

    private static final Logger log = LoggerFactory.getLogger(RolesController.class);

    private final RolesService rolesService;

    @Autowired
    public RolesController(RolesService rolesService) {
        this.rolesService = rolesService;
    }

    @OperationLog(type = "list", module = "role", description = "获取所有角色列表")
    @GetMapping("/get")
    public AjaxResponse getRolesList() {
        try {
            log.info("开始获取角色列表");
            if (rolesService == null) {
                log.error("rolesService is null");
                return AjaxResponse.error(500, "服务未正确初始化");
            }
            List<Roles> roles = rolesService.list();
            log.info("成功获取角色列表，数量: {}", roles.size());
            return AjaxResponse.success(200, "查询成功", roles);
        } catch (Exception e) {
            log.error("获取角色列表失败: {}", e.getMessage(), e);
            return AjaxResponse.error(500, "获取角色列表失败: " + e.getMessage());
        }
    }
}
